//
//  jiaoyiMingxiTableViewController.m
//  HaoDeCaiFu
//
//  Created by 物联信息中心 on 2018/1/4.
//  Copyright © 2018年 郑开宏. All rights reserved.
//

#import "jiaoyiMingxiTableViewController.h"
#import "makeUpTableViewCell.h"
#import "GeneralMagager.h"
@interface jiaoyiMingxiTableViewController ()
@property(nonatomic,assign)NSInteger page;
@end

@implementation jiaoyiMingxiTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"交易明细";
    self.navigationController.navigationBarHidden=NO;
     [self.tableView registerNib:[UINib nibWithNibName:@"makeUpTableViewCell" bundle:nil] forCellReuseIdentifier:@"makeUpTableViewCell"];
    [self.tableView setTableFooterView:[[UIView alloc]initWithFrame:CGRectZero]];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mainRefresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(mainLoadMore)];
    self.page=1;
    self.tableView.rowHeight=80;
    [self getJiaoyiDataWithIsRfresh:NO];
}

-(void)mainRefresh
{
    self.page=1;
    [self getJiaoyiDataWithIsRfresh:NO];
}


-(void)mainLoadMore
{
    self.page++;
    [self getJiaoyiDataWithIsRfresh:YES];
}


-(void)getJiaoyiDataWithIsRfresh:(BOOL)IsRfresh
{
    
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"accountNumber"] = kAcount;
    
    paramets[@"pageSize"] = @"10";
    paramets[@"pageIndex"] = [NSString stringWithFormat:@"%ld", (long)self.page];
    
  
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:@"/public/wallet/get-cash-wallet-order-info"};
    
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        
        //NSLog(@"%@",result);
        
        NSString*code=[result objectForKey:@"code"];
        
        
        if ([code isEqualToString:@"0000"]) {
            
           
            NSDictionary*dataDic=[result objectForKey:@"data"];
           
            
            if (IsRfresh==YES) {
                 ZYjiaoyi*jiaoyi=[ZYjiaoyi mj_objectWithKeyValues:dataDic];
                [self.jiaoyi.data addObjectsFromArray:jiaoyi.data];
            }
            else
            {
                 self.jiaoyi=[ZYjiaoyi mj_objectWithKeyValues:dataDic];
            }
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
            
            
            
        }
        else
        {
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [self.tableView.mj_footer endRefreshing];
            [self.tableView.mj_header endRefreshing];
        }
        
        
    } failure:^(NSError *errer) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jiaoyi.data.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    makeUpTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"makeUpTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[makeUpTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"makeUpTableViewCell"];
        
        
    }
    
    ZYjiaoyi*jiaoyi=self.jiaoyi.data[indexPath.row];
    cell.name.text=jiaoyi.transactionType;
    cell.numStyle.text=jiaoyi.transactionStatus;
    cell.shouyi.text=[NSString stringWithFormat:@"%@元",jiaoyi.transactionAmount];
    cell.zhanbi.text=[GeneralMagager shijianChuoZhuanshijianWith:jiaoyi.orderDate];
    
    return cell;
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
